﻿using System;
using System.IO;
using System.Linq;
using Lucene.Linq.VSTests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Lucene.Linq.Storage.EntityFramework;
using Lucene.Linq.VSTests.DatabaseIntegration.LinqToSql;

namespace Lucene.Linq.VSTests
{
    [TestClass]
    public class TestDatabaseIndexSet
    {
        #region Generators

        private static readonly Random random = new Random();

        private string RandomPath()
        {
            return Path.Combine(Environment.CurrentDirectory, "index_" + random.String(30));
        }

        #endregion

        #region Ctors

        [TestMethod, ExpectedException(typeof (ArgumentNullException))]
        public void Construct_should_throw_given_null_data_context()
        {
            // db set should instantiate the db instead

            new DatabaseIndexSet<NorthwindDataContext>(null);

            new DatabaseIndexSet<NorthwindDataContext>(RandomPath(), null);

            new DatabaseIndexSet<NorthwindDataContext>(new DirectoryInfo(RandomPath()), null);
        }

        [TestMethod]
        public void Construct_should_add_indexes_to_set()
        {
            // db set should instantiate the db instead
            var dbi = new DatabaseIndexSet<NorthwindDataContext>(new NorthwindDataContext());
            Assert.IsNotNull(dbi);
            Assert.IsNotNull(dbi.DataContext);
            Assert.IsTrue(dbi.Count == 2);

            dbi = new DatabaseIndexSet<NorthwindDataContext>(RandomPath(), new NorthwindDataContext());
            Assert.IsNotNull(dbi);
            Assert.IsNotNull(dbi.DataContext);
            Assert.IsTrue(dbi.Count == 2);

            dbi = new DatabaseIndexSet<NorthwindDataContext>(new DirectoryInfo(RandomPath()), new NorthwindDataContext());
            Assert.IsNotNull(dbi);
            Assert.IsNotNull(dbi.DataContext);
            Assert.IsTrue(dbi.Count == 2);
        }

        [TestMethod]
        public void Construct_on_data_context_with_missing_document_attributes_should_add_only_relevant_indexes_to_set()
        {
            // db set should instantiate the db instead
            var dbi = new DatabaseIndexSet<VSTests.Data.LinqToSql.NorthwindDataContext>(new VSTests.Data.LinqToSql.NorthwindDataContext());
            Assert.IsNotNull(dbi.DataContext);
            Assert.IsTrue(dbi.Count == 1);
            Assert.IsTrue(dbi[typeof (VSTests.Data.LinqToSql.Customer)].Name == "Customer");
        }

        [TestMethod, ExpectedException(typeof (ArgumentNullException))]
        public void Construct_throws_given_null_path_or_directory()
        {
            var dbi1 = new DatabaseIndexSet<NorthwindDataContext>("",
                                                                  new NorthwindDataContext());


            var dbi2 = new DatabaseIndexSet<NorthwindDataContext>((string) null,
                                                                  new NorthwindDataContext());


            var dbi3 = new DatabaseIndexSet<NorthwindDataContext>((DirectoryInfo) null,
                                                                  null);
        }

        #endregion

        #region Write

        [TestMethod]
        [Description("This indexes the Customer and Orders tables of the Northwind database and will take a while")]
        public void Write_all_should_add_to_index()
        {
            var dbi = new DatabaseIndexSet<NorthwindDataContext>(new NorthwindDataContext());
            dbi.Write();

            // check that all rows where added as documents to each index
            Assert.IsTrue(
                dbi.Get<Customer>().Count == dbi.DataContext.Customers.Count()
                );

            Assert.IsTrue(
                dbi.Get<Order>().Count == dbi.DataContext.Orders.Count()
                );
        }

        #endregion
    }
}